<?php

namespace Database\Seeders;

use App\Models\User;
use Carbon\Carbon;
use App\Models\GoldCoin;
use App\Models\GoldRecord;
use App\Models\GoldProduct;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class ReturnExceptionCoins extends Seeder
{
	protected function repeatedCoins()
	{
		$allItems = collect();
		$repeatItems = collect();
		$endItems = collect();
		
		foreach (GoldCoin::where('user_id', 22810)->get() as $coin) {
			
			if ($coin->remark != 'pay_gold_record') continue;
			
			$i = $allItems->where('user_id', $coin->user_id)
				->where('record_id', $coin['data']['gold_record_id'])->first();
			
			if ($i) {
				$repeatItems->push([
					'coin_id' => $i['coin_id'],
					'user_id' => $i['user_id'],
					'record_id' => $i['record_id'],
					'money' => $i['money'],
					'coin_id_repeat' => $coin->id,
					'user_id_repeat' => $coin->user_id,
					'record_id_repeat' => $coin->data['gold_record_id'],
					'money_repeat' => $coin->money,
				]);
			} else {
				$allItems->push([
					'coin_id' => $coin->id,
					'user_id' => $coin->user_id,
					'record_id' => $coin->data['gold_record_id'],
					'money' => $coin->money,
				]);
			}
		}
		
		return $repeatItems;
	}
	
	protected function backCoinsToUser()
	{
		$repeats = $this->repeatedCoins();
		
		foreach ($repeats as $repeat) {
			$user = User::find(22810);
			$money = abs($repeat['money']);
			$data = ['gold_record_id' => $repeat['record_id']];
			account('gold_coin', $user)->plusExtraGoldCoins($money, $data);
			\Log::info("Back to user: {$user->id} : {$money}");
		}
	}
	
	protected function backOne()
	{
		$user = User::find(22940);
		$money = 148.08;
		account('gold_coin', $user)->plusExceptionCoinsRefund($money);
		\Log::info("Back to user: {$user->id} : {$money}");
	}
	
	public function run()
	{
		$this->backOne();
	}
}
